package com.ssm.studen.controller;

import com.ssm.studen.model.Student;
import com.ssm.studen.service.StudentService;
import com.ssm.studen.tool.ResponseMessage;
import com.ssm.studen.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/***
 * 学生表：
 *      对外提供数据接口服务
 */
@CrossOrigin
@Api(tags = "student")
@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**********************学生添加方法*************************/
    @ApiOperation("学生添加方法")
    @RequestMapping(value = "/addStudent",method = RequestMethod.GET)
    public ResponseMessage addStudent(Student student){
        int isAdd = 0;
        //传入数据判断
        //1.学生对象不能为空
        if(student != null){
            //2.学生基本信息不能为空
            if(student.getSid()!=null){
                //3.添加学生数据
                isAdd = studentService.addStudent(student);
                if(isAdd == 1){
                    return Result.success("保存成功");
                }else if(isAdd == -1){
                    return Result.error("学生重复不允许添加！");
                }else{
                    return Result.error("保存失败");
                }
            }
        }
        return Result.error("保存失败");
    }

    /**********************学生删除方法*************************/
    @ApiOperation("学生删除方法")
    @RequestMapping(value = "/deleteStudent",method = RequestMethod.GET)
    public ResponseMessage deleteStudent(String sid){
        if(sid != null){
            int delInt = 0;
            delInt = studentService.delStudent(sid);
            if(delInt>0){
                return Result.success("删除成功");
            }else{
                return Result.error("删除失败");
            }
        }else{
            return Result.error("删除的主键不能为“0”，请重新输入！");
        }
    }


    /**********************学生更新方法*************************/
    @ApiOperation("学生更新方法")
    @RequestMapping(value = "/updataStudent",method = RequestMethod.GET)
    public ResponseMessage updataStudent(Student student){
        //传入的参数不能为空
        if(student != null){
            //参数当中主键不能为0
            if(student.getSnum() != 0){
                if(studentService.updateStudent(student) == 1){
                    if(student.getSid()!=null||student.getSclass()!=null||student.getSmarjor()!=null){
                        studentService.updateStudent(student);
                    }else{
                        return Result.error("更新失败");
                    }
                }else{
                    return Result.error("更新失败");
                }
            }
        }
        return Result.error("传入参数不正确");
    }
}


















